import { createRouter, createWebHashHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/dashboard',
    component: () => import('../views/Layout.vue'),
    meta: { title: '首页' },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { title: '仪表盘' }
      },
      // 商品管理
      {
        path: 'product',
        name: 'ProductManagement',
        redirect: '/product/list',
        meta: { title: '商品管理' },
        children: [
          {
            path: 'list',
            name: 'ProductList',
            component: () => import('../views/product/ProductList.vue'),
            meta: { title: '商品列表' }
          },
          {
            path: 'add',
            name: 'ProductAdd',
            component: () => import('../views/product/ProductEdit.vue'),
            meta: { title: '添加商品' }
          },
          {
            path: 'edit/:id',
            name: 'ProductEdit',
            component: () => import('../views/product/ProductEdit.vue'),
            meta: { title: '编辑商品' }
          }
        ]
      },
      // 商店管理
      {
        path: 'store',
        name: 'StoreManagement',
        redirect: '/store/list',
        meta: { title: '商店管理' },
        children: [
          {
            path: 'list',
            name: 'StoreList',
            component: () => import('../views/store/StoreList.vue'),
            meta: { title: '商店列表' }
          },
          {
            path: 'add',
            name: 'StoreAdd',
            component: () => import('../views/store/StoreEdit.vue'),
            meta: { title: '添加商店' }
          },
          {
            path: 'edit/:id',
            name: 'StoreEdit',
            component: () => import('../views/store/StoreEdit.vue'),
            meta: { title: '编辑商店' }
          }
        ]
      },
      // 视频管理
      {
        path: 'video',
        name: 'VideoManagement',
        redirect: '/video/list',
        meta: { title: '视频管理' },
        children: [
          {
            path: 'list',
            name: 'VideoList',
            component: () => import('../views/video/VideoList.vue'),
            meta: { title: '视频列表' }
          },
          {
            path: 'add',
            name: 'VideoAdd',
            component: () => import('../views/video/VideoEdit.vue'),
            meta: { title: '上传视频' }
          },
          {
            path: 'edit/:id',
            name: 'VideoEdit',
            component: () => import('../views/video/VideoEdit.vue'),
            meta: { title: '编辑视频' }
          }
        ]
      },
      // 会员管理
      {
        path: 'user',
        name: 'UserManagement',
        redirect: '/user/list',
        meta: { title: '会员管理' },
        children: [
          {
            path: 'list',
            name: 'UserList',
            component: () => import('../views/user/UserList.vue'),
            meta: { title: '会员列表' }
          },
          {
            path: 'detail/:id',
            name: 'UserDetail',
            component: () => import('../views/user/UserDetail.vue'),
            meta: { title: '会员详情' }
          }
        ]
      },
      // AI对话管理
      {
        path: 'chat',
        name: 'ChatManagement',
        redirect: '/chat/list',
        meta: { title: 'AI对话管理', icon: 'el-icon-chat-dot-round' },
        children: [
          {
            path: 'list',
            name: 'ConversationList',
            component: () => import('../views/chat/ConversationList.vue'),
            meta: { title: '对话列表', icon: 'el-icon-s-order' }
          },
          {
            path: 'detail/:id',
            name: 'ConversationDetail',
            component: () => import('../views/chat/ConversationDetail.vue'),
            meta: { title: '对话详情', icon: 'el-icon-document' },
            hidden: true
          },
          {
            path: 'ai-responses',
            name: 'AiResponseList',
            component: () => import('../views/chat/AiResponseList.vue'),
            meta: { title: 'AI回复内容', icon: 'el-icon-data-analysis' }
          },
          {
            path: 'ai-response/:id',
            name: 'AiResponseDetail',
            component: () => import('../views/chat/AiResponseDetail.vue'),
            meta: { title: 'AI回复详情', icon: 'el-icon-document' },
            hidden: true
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录' }
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')

  // 如果前往登录页，直接通过
  if (to.path === '/login') {
    next()
    return
  }

  // 如果没有登录，重定向到登录页
  if (!token) {
    next('/login')
    return
  }

  next()
})

export default router 